/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.HomeImage;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.HomeImageRepository;
import me.zhengjie.modules.biz.service.HomeImageService;
import me.zhengjie.modules.biz.service.dto.HomeImageDto;
import me.zhengjie.modules.biz.service.dto.HomeImageQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.HomeImageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-05-12
**/
@Service
@RequiredArgsConstructor
public class HomeImageServiceImpl implements HomeImageService {

    private final HomeImageRepository homeImageRepository;
    private final HomeImageMapper homeImageMapper;

    @Override
    public Map<String,Object> queryAll(HomeImageQueryCriteria criteria, Pageable pageable){
        Page<HomeImage> page = homeImageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(homeImageMapper::toDto));
    }

    @Override
    public List<HomeImageDto> queryAll(HomeImageQueryCriteria criteria){
        return homeImageMapper.toDto(homeImageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public HomeImageDto findById(Integer id) {
        HomeImage homeImage = homeImageRepository.findById(id).orElseGet(HomeImage::new);
        ValidationUtil.isNull(homeImage.getId(),"HomeImage","id",id);
        return homeImageMapper.toDto(homeImage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HomeImageDto create(HomeImage resources) {
        return homeImageMapper.toDto(homeImageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(HomeImage resources) {
        HomeImage homeImage = homeImageRepository.findById(resources.getId()).orElseGet(HomeImage::new);
        ValidationUtil.isNull( homeImage.getId(),"HomeImage","id",resources.getId());
        homeImage.copy(resources);
        homeImageRepository.save(homeImage);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            homeImageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<HomeImageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HomeImageDto homeImage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" imageUrl",  homeImage.getImageUrl());
            map.put(" createdAt",  homeImage.getCreatedAt());
            map.put(" updatedAt",  homeImage.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}